##################################################################
# This file explores the litigation patterns of inmates filing
# against private prison companies.
##################################################################
#set working directory
setwd("/Users/annagunderson/Dropbox/Private Lawsuits/JLC replication files")
#clear working environment
rm(list=ls())
#load packages
library(readxl)
library(tidyverse)
library(reshape2)
library(plyr)
library(dplyr)
library(stargazer) 
library(xtable)
library(lfe)
library(MatchIt)
library(cobalt)
library(vtable)

##################################################################
# Import data --------------
##################################################################  
#filter by those filed against private companies
#names from my understanding, and McDonald (1998) report on
#Management Firms who Operate Facilities Reported in NCCD Survey
#and Abt Associates Survey and Number of Privately Operated Facilities, by Ownership
lawsuits <- read.csv("./data/fjcbloombergmerge.csv")%>%
  na_if(., -8) %>% filter(NOSedit==550 | NOSedit==555) %>% filter(File.Year>1985) %>%
  group_by(Term.Year,DISTRICT) %>% 
  dplyr::mutate(g = group_indices()) %>% #interacted year-district effects 
  ungroup() %>% distinct() %>% group_by(NOSedit,Docket,File.Year,File.Month,File.Day,Term.Year,Term.Day,Term.Month)%>%
  filter(n()==1) %>% ungroup() %>%
  mutate(private_publiclytraded = as.integer(str_detect(tolower(DEF), paste(c(" cca ", "c c a","c.c.a.","cca ","cca - ",
                                                                              "director of cca","medical staff, cca","owner cca", "/ cca,","superintendent of cca","cca prison","ceo of cca",
                                                                              "cca correction","/cca,","corrections co ","corrections crop",
                                                                              "cca-","cca/","cca,","\\(cca\\)","wackenhut", "geo group", "geo care","geo corp", "geo facility","geo corrections",
                                                                              "geo department","geo facility","geo, et","geo, inc",
                                                                              "geo inc", "geo intermediate","geo international","geo medical","geo world","geo,","geo-","geo/",
                                                                              "wachenhut","wacken hut","wackenhot",
                                                                              "corrections corp","correction corp","correctional corp",
                                                                              "cornell companies","cornell corrections","cornell chairman","cornell corections","cornell corporation",
                                                                              "cornell corr","cornell inc","cornell, inc",
                                                                              "esmor ","esmor, inc","esmor: ","lasalle geo", "corecivic","prison realty"),collapse = '|'))),
         private=as.integer(str_detect(tolower(DEF), paste(c(" cca ", "c c a","c.c.a.","cca ","cca - ",
                                                             "director of cca","medical staff, cca","owner cca", "/ cca,","superintendent of cca","cca prison","ceo of cca",
                                                             "cca correction","/cca,","corrections co ","corrections crop",
                                                             "cca-","cca/","cca,","\\(cca\\)","wackenhut", "geo group", "geo care","geo corp", "geo facility","geo corrections",
                                                             "geo department","geo facility","geo, et","geo, inc",
                                                             "geo inc", "geo intermediate","geo international","geo medical","geo world","geo,","geo-","geo/",
                                                             "wachenhut","wacken hut","wackenhot",
                                                             "corrections corp","correction corp","correctional corp",
                                                             "cornell companies","cornell corrections","cornell chairman","cornell corections","cornell corporation",
                                                             "cornell corr","cornell inc","cornell, inc",
                                                             "esmor ","esmor, inc","esmor: ","lasalle geo", "corecivic","prison realty", 
                                                             "mtc ", "management and ","lasalle management","lasalle corrections",
                                                             "lasalle geo", "corecivic","bobby ross","correctional services","civigenics","alternative programs",
                                                             "capital corrections","grw corp","prison realty","marantha", "louisiana corrections",
                                                             "turning point ","corrections services","dove development","industrial development authority",
                                                             "delta correctional","appelton prison","mooreland corp","correctional facility corporation",
                                                             "emerald corrections"),
                                                           collapse = '|'))),
         prosecutor = as.integer(str_detect(tolower(employment.text.field.),paste(c("prosecut","district attorney","commonwealth attorney","state attorney"),collapse="|"))),
         somewinnings = ifelse(NOJ!=0,1,0),favjudgment = ifelse(JUDGMENT==1 | JUDGMENT==3,1,0),
         favjudgmentsettlements=ifelse(JUDGMENT==1 | JUDGMENT == 3 | DISP ==5 | DISP==12 | DISP==13,1,0),
         prosedef = ifelse(PROSE==1 | PROSE==3,1,0), dismissed = ifelse(DISP==3 | DISP==14,1,0),
         dismissed.strict=ifelse(DISP==3,1,0),dismissed.kindastrict=ifelse(DISP==3 | DISP==14,1,0),
         injunction = ifelse(NOJ==3,1,0), monetaryaward = ifelse(AMTREC>0,1,0), CIRCUIT = as.factor(CIRCUIT),
         DISTRICT = as.factor(DISTRICT), File.Year = as.factor(File.Year), settled = ifelse(DISP==13,1,0), 
         classaction = ifelse(CLASSACT==1,1,0), blackjudge=ifelse(race.or.ethnicity.=="African American",1,0), 
         reppres = ifelse(party.affiliation.of.president.=="Republican",1,0), male=ifelse(gender.=="M",1,0), 
         rep = ifelse(imputed.dime.cfscore.>0,1,0), dem = ifelse(imputed.dime.cfscore.<0,1,0),
         morethanoneplaintiff = as.integer(str_detect(PLT, "ET AL")),
         filetotermdiff = as.numeric(as.Date(as.character(TERMDATE), format="%Y-%m-%d")-as.Date(as.character(FILEDATE), format="%Y-%m-%d")),
         Operation = ifelse(private_publiclytraded==1,"Private","Public"),
         publicdefender = as.integer(str_detect(tolower(employment.text.field.),"public defend")))

##################################################################
# Matching --------------
##################################################################  
lawsuits_nona <- lawsuits %>%
  select(File.Year,private,private_publiclytraded,Docket,CIRCUIT,TITL,DISTRICT,OFFICE,ORIGIN,JURIS,NOSedit,SECTION,JUDGMENT,DISP,CASENAME,favjudgment,dismissed) %>%
  na.omit() 

mod_match <- matchit(private_publiclytraded ~ File.Year+CIRCUIT+DISTRICT+OFFICE+ORIGIN+NOSedit+SECTION+JURIS+TITL, data = lawsuits_nona,
                     method = "exact")  

match <- match.data(mod_match)   

matched_sample_full <- left_join(match,lawsuits)

##################################################################
# OLS model function --------------
##################################################################  
olsmodel <- function(tabletitle,tablelabel,data){
  favjudgment1 <- felm(favjudgment~private_publiclytraded|
                         as.factor(g)|0|DISTRICT,data=data,weights=data$weights)
  
  favjudgment2 <- felm(favjudgmentsettlements~private_publiclytraded|
                         as.factor(g)|0|DISTRICT,data=data,weights=data$weights) 
  
  dismissed1 <- felm(dismissed~private_publiclytraded|
                       as.factor(g)|0|DISTRICT,data=data,weights=data$weights)
  
  favjudgment3 <- felm(favjudgment~private_publiclytraded+prosedef|
                         as.factor(g)|0|DISTRICT,data=data,weights=data$weights)
  
  favjudgment4 <- felm(favjudgmentsettlements~private_publiclytraded+prosedef|
                         as.factor(g)|0|DISTRICT,data=data,weights=data$weights) 
  
  dismissed2 <- felm(dismissed~private_publiclytraded+prosedef|
                       as.factor(g)|0|DISTRICT,data=data,weights=data$weights)
  
  stargazer(favjudgment1,favjudgment2,dismissed1,favjudgment3,favjudgment4,dismissed2,
            dep.var.labels = c("Fav. Judgment", "Likely Fav. Judgment",
                               "Case Dismissed","Fav. Judgment", "Likely Fav. Judgment",
                               "Case Dismissed"),
            covariate.labels = c("Private Prison Defendant",
                                 "Pro Se Plaintiff"),title=tabletitle,
            label = paste0("olsmain",tablelabel), style="apsr",
            notes="All standard errors clustered by district. Interacted district-year fixed effects included. Observations are weighted.",
            out=paste0("./tables/",tablelabel,".tex"),
            font.size="tiny",df=F)
}

##################################################################
# Figures --------------
##################################################################  
#Figure 1
private <- read_excel("./data/facilitydatabase.xlsx") %>%
  group_by(year) %>%
  dplyr::summarise(sumprivate=sum(designcapacity,na.rm=T)) 

load(file="./Data/prisoners7816.rda")
public <- da37003.0001 %>%
  group_by(YEAR) %>%
  dplyr::summarise(sumall=sum(JURTOTM,JURTOTF,na.rm=T)) %>% dplyr::rename(year=YEAR)

rm(da37003.0001)

publicandprivate <- full_join(private,public) %>% mutate(sumpublic=sumall-sumprivate)

all <- left_join(lawsuits %>%
                   group_by(Term.Year,Operation) %>%
                   dplyr::summarise(n=n()) %>% dplyr::rename(year=Term.Year) %>%
                   filter(!is.na(Operation)) %>% spread(.,Operation,n),publicandprivate) %>%
  mutate(privatepercapita=(Private/sumprivate)*100000,publicpercapita=(Public/sumpublic)*100000,
         privatepercapitaguess=(Private*2/sumprivate)*100000)

all_graph <- gather(all, Operator, measurement, privatepercapita:publicpercapita, factor_key=TRUE)
ggplot(all_graph,aes(x=year,y=measurement,color=Operator))+geom_point()+
  theme_bw()+
  scale_color_manual(labels = c("Private", "Public"), values = c("black", "lightgray"))+ylab("Filings Per Capita")+
  xlab("Year")
ggsave("./figures/Figure1.pdf")

##################################################################
# Tables --------------
##################################################################  
#Table 1
filingrates <- t.test(all$privatepercapita,all$publicpercapita) 

t.filingrates <- data.frame("t-statistic"=filingrates$statistic,
                            "Confidence Interval"=paste0(round(filingrates$conf.int[[1]],2),", ",
                                                         round(filingrates$conf.int[[2]],2)), 
                            "p-value" = filingrates$p.value)

print(xtable(t.filingrates,caption=paste0("Results from a Welch Two Sample t-test. Approximately ",
                                          (round(filingrates$estimate[[1]],2))," lawsuits are filed per 100,000 inmates in private prisons and ",
                                          (round(filingrates$estimate[[2]],2))," lawsuits are filed per 100,000 inmates in public prisons."),
             label="ttestfilingrates"),digits=2,file="./tables/table1.tex")

#Table 2
cases_sample <- read_excel("./data/lawsuitscode.xlsx")

private <- cases_sample %>% filter(private_publiclytraded==1) %>% filter(complaint==1)
public <- cases_sample %>% filter(private_publiclytraded==0) %>% filter(complaint==1)

t.legal <- t.test(private$legal.complaint,public$legal.complaint)
t.legalaccess <- t.test(private$lackofaccess.complaint,public$lackofaccess.complaint)

cases_table <- cases_sample %>% filter(complaint==1) %>%
  mutate(Operation=ifelse(private_publiclytraded==1,"Private","Public")) %>% 
  group_by(Operation) %>%
  dplyr::summarise(Sampled=50,`Dockets Available`=n(),
                   `Legal Access Complaints`=sum(lackofaccess.complaint,na.rm=T),
                   `Percent Legal Access Complaints`=(sum(lackofaccess.complaint,na.rm=T)/n())*100)

print(xtable(cases_table, caption= paste0("Comparing complaints against public and publicly traded private prisons. The p-value of the t-test comparing the legal access complaints is ",round(t.legalaccess$p.value,2),"."), 
             label="casestable",digits=c(0,0,0,0,0,1)),size="footnotesize",
      file="./tables/table2.tex")  

#Table 3
a <- summary(mod_match)
matchingtable <- as.data.frame(a$nn) %>%
  slice(-1,-3,-6)

print(xtable(matchingtable, caption= "Exact Matching Sample", 
             label="matchingtable",digits=c(0,0,0)),file="./tables/table3.tex")  

#Table 4
st(matched_sample_full, group = 'Operation', vars=c('filetotermdiff','prosedef','favjudgment','favjudgmentsettlements','dismissed'),
   group.test = TRUE,out="latex",labels=c("Length to Termination (Days)","Pro Se Plaintiff",
                                          "Favorable Judgment","Likely Favorable Judgment","Dismissed"),
   file="./tables/table4.tex",title="Summary Statistics of Private and Public Prison Litigants, Matched Sample",
   anchor="sumstatsmatched",digits=2)

#Table 5
olsmodel("Inmate Lawsuits Filed Against Public and Private Prisons Operated by Publicly Traded Companies, A Comparison","table5",matched_sample_full)

##################################################################
# Appendix materials --------------
##################################################################  
#Table 1
favjudgment1 <- felm(favjudgment~private_publiclytraded|
                       as.factor(g)|0|DISTRICT,data=matched_sample_full)

favjudgment2 <- felm(favjudgmentsettlements~private_publiclytraded|
                       as.factor(g)|0|DISTRICT,data=matched_sample_full) 

dismissed1 <- felm(dismissed~private_publiclytraded|
                     as.factor(g)|0|DISTRICT,data=matched_sample_full)

favjudgment3 <- felm(favjudgment~private_publiclytraded+imputed.dime.cfscore.+prosecutor+blackjudge+male+birth.year.+prosedef|
                       as.factor(g)|0|DISTRICT,data=matched_sample_full)

favjudgment4 <- felm(favjudgmentsettlements~private_publiclytraded+imputed.dime.cfscore.+prosecutor+blackjudge+male+birth.year.+prosedef|
                       as.factor(g)|0|DISTRICT,data=matched_sample_full) 

dismissed2 <- felm(dismissed~private_publiclytraded+imputed.dime.cfscore.+prosecutor+blackjudge+male+birth.year.+prosedef|
                     as.factor(g)|0|DISTRICT,data=matched_sample_full)

stargazer(favjudgment1,favjudgment2,dismissed1,favjudgment3,favjudgment4,dismissed2,
          dep.var.labels = c("Fav. Judgment", "Likely Fav. Judgment",
                             "Case Dismissed","Fav. Judgment", "Likely Fav. Judgment",
                             "Case Dismissed"),
          covariate.labels = c("Private Prison Defendant","Judge Ideology (Bonica and Sen)","Judge Former Prosecutor",
                               "Black Judge","Male Judge","Judge Birth Year",
                               "Pro Se Plaintiff"),title="Inmate Lawsuits Filed Against Public and Private Prisons Operated by Publicly Traded Companies, A Comparison (Without Weights)",
          label = "olsmainnoweight", style="apsr",
          notes="All standard errors clustered by district. Interacted district-year fixed effects included.",
          out="./tables/appendixtable1.tex",
          font.size="tiny",df=F)

#Table 2
favjudgment1 <- felm(favjudgment~private_publiclytraded|
                       as.factor(g)|0|DISTRICT,data=lawsuits)

favjudgment2 <- felm(favjudgmentsettlements~private_publiclytraded|
                       as.factor(g)|0|DISTRICT,data=lawsuits) 

dismissed1 <- felm(dismissed~private_publiclytraded|
                     as.factor(g)|0|DISTRICT,data=lawsuits)

favjudgment3 <- felm(favjudgment~private_publiclytraded+imputed.dime.cfscore.+prosecutor+blackjudge+male+birth.year.+prosedef|
                       as.factor(g)|0|DISTRICT,data=lawsuits)

favjudgment4 <- felm(favjudgmentsettlements~private_publiclytraded+imputed.dime.cfscore.+prosecutor+blackjudge+male+birth.year.+prosedef|
                       as.factor(g)|0|DISTRICT,data=lawsuits) 

dismissed2 <- felm(dismissed~private_publiclytraded+imputed.dime.cfscore.+prosecutor+blackjudge+male+birth.year.+prosedef|
                     as.factor(g)|0|DISTRICT,data=lawsuits)

stargazer(favjudgment1,favjudgment2,dismissed1,favjudgment3,favjudgment4,dismissed2,
          dep.var.labels = c("Fav. Judgment", "Likely Fav. Judgment",
                             "Case Dismissed","Fav. Judgment", "Likely Fav. Judgment",
                             "Case Dismissed"),
          covariate.labels = c("Private Prison Defendant","Judge Ideology (Bonica and Sen)","Judge Former Prosecutor",
                               "Black Judge","Male Judge","Judge Birth Year",
                               "Pro Se Plaintiff"),title="Inmate Lawsuits Filed Against Public and Private Prisons Operated by Publicly Traded Companies, A Comparison (Without Matching)",
          label = "olsmainnomatching", style="apsr",
          notes="All standard errors clustered by district. Interacted district-year fixed effects included.",
          out="./tables/appendixtable2.tex",
          font.size="tiny",df=F)

#Table 3
termlength1 <- felm(log(1+filetotermdiff)~private_publiclytraded|
                      as.factor(g)|0|DISTRICT,data=matched_sample_full,weights=matched_sample_full$weights) 

termlength2 <- felm(log(1+filetotermdiff)~private_publiclytraded+imputed.dime.cfscore.+prosecutor+blackjudge+male+birth.year.+prosedef|
                      as.factor(g)|0|DISTRICT,data=matched_sample_full,weights=matched_sample_full$weights) 

stargazer(termlength1,termlength2, 
          dep.var.labels = "Length to Termination (Logged Days)",
          covariate.labels = c("Private Prison Defendant","Judge Ideology (Bonica and Sen)","Judge Former Prosecutor",
                               "Black Judge","Male Judge","Judge Birth Year",
                               "Pro Se Plaintiff"),title="Inmate Lawsuits Filed Against Public and Private Prisons Operated by Publicly Traded Companies, A Comparison (Using Length to Termination as a Dependent Variable)",
          label = "olsmaintermlength", style="apsr",
          notes="All standard errors clustered by district. Interacted district-year fixed effects included.",
          out="./tables/appendixtable3.tex",
          font.size="small",df=F)
